﻿using Abp.Application.Services;
using BRMC.IntelligentCommunity.Parkings.ParkingsBillingSchemes.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BRMC.IntelligentCommunity.IntelligentCommunity.Parkings;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Microsoft.EntityFrameworkCore;
using Abp.Linq.Extensions;

namespace BRMC.IntelligentCommunity.Parkings
{
    public class ParkingsBillingSchemeAppService : AsyncCrudAppService<
              ParkingsBillingScheme,
             ParkingsBillingSchemeListDto,
              long,
               GetBillingSchemeInput,
              CreateParkingsBillingScheme,
              CreateParkingsBillingScheme
              >, IParkingsBillingSchemeAppService
    {
        private readonly IRepository<ParkingsBillingScheme, long> _repository;
        private readonly IRepository<UserParkinginfo, long> _iUserParkinginfoRepository;
        private readonly IRepository<CarInfo, long> _iCarInfoRepository;
        private readonly IRepository<CarLogHistory, long> _iCarLogHistoryRepository;
        private readonly IRepository<Place, long> _iPlaceRepository;
        private readonly IRepository<Paymethod, long> _paymethodRepository;

        public ParkingsBillingSchemeAppService(IRepository<ParkingsBillingScheme, long> repository,
            IRepository<UserParkinginfo, long> iUserParkinginfoRepository,
            IRepository<CarInfo, long> iCarInfoRepository,
            IRepository<CarLogHistory, long> iCarLogHistoryRepository,
            IRepository<Place, long> iPlaceRepository,
            IRepository<Paymethod, long> paymethodRepository) : base(repository)
        {

            _repository = repository;
            _iUserParkinginfoRepository = iUserParkinginfoRepository;
            _iCarInfoRepository = iCarInfoRepository;
            _iCarLogHistoryRepository = iCarLogHistoryRepository;
            _iPlaceRepository = iPlaceRepository;
            _paymethodRepository = paymethodRepository;
        }


        protected override IQueryable<ParkingsBillingScheme> CreateFilteredQuery(GetBillingSchemeInput input)
        {
            return base.CreateFilteredQuery(input)
                .Where(c => c.ParkingId == input.ParkingId)
                .WhereIf(input.Enable.HasValue, e => e.Enable == input.Enable.Value);

        }
        public async Task<GetBillingSchemeForEditOutput> GetBillingSchemeForEdit(NullableIdDto input)
        {
            ParkingsBillingScheme parkingsBillingScheme = null;
            if (input.Id.HasValue)
            {
                parkingsBillingScheme = await _repository.GetAsync(input.Id.Value);
            }

            var output = new GetBillingSchemeForEditOutput();

            output.BillingSchemeEdit = parkingsBillingScheme != null ? ObjectMapper.Map<ParkingsBillingSchemeEdit>(parkingsBillingScheme) : new ParkingsBillingSchemeEdit();

            return output;
        }

        #region 
        /*
        /// <summary>
        /// 计算收费金额
        /// </summary>
        /// <param name="parkingId">停车场Id</param>
        /// <param name="carId">车辆Id</param>
        /// <param name="carType">0：临时车，其他值均为非临时车</param>
        /// <param name="inTime">入场时间</param>
        /// <param name="outTime">出场时间或预交费时间</param>
        /// <param name="carLogHistoryId"></param>
        /// <returns></returns>
        public async Task<CostAndRemark> GetCost(long parkingId, long carId,int carType, DateTime inTime,DateTime outTime, long carLogHistoryId)
        {
            var costAndRemark = new CostAndRemark { Cost = 0, Remark = "" };
            try
            {                
                outTime = Convert.ToDateTime(outTime);
                if (outTime < inTime)
                {
                    costAndRemark.Remark = "出场时间小于等于入场时间，入场时间：" + inTime.ToString("yyyy-MM-dd HH:mm:ss") + ",出场时间：" + outTime.ToString("yyyy-MM-dd HH:mm:ss");
                    return costAndRemark;
                }
                inTime = Convert.ToDateTime(inTime.ToString("yyyy-MM-dd HH:mm"));

                var paymethod = await _paymethodRepository.GetAll()
                .GroupBy(e => e.CarLogHistoryId)
                .Where(e => e.Key == carLogHistoryId)
                .Select(e => new { LastCreationTime = e.Max(m => m.CreationTime), SumCost = e.Sum(s => s.Cost) })
                .FirstOrDefaultAsync();

                //没有交费信息
                if (paymethod == null || paymethod.SumCost == 0)
                {

                    //计费原则：1. 进场为临时车时按临时车计费，2. 进场为月租车时，如果未超期费用为0，如果超期按临时车收费，收费开始时间为车位到期时间加一分钟。            
                    if (carType != 0)
                    {
                        //车辆入场或出场时的有效月租车车位信息
                        var carPlaceList = await _iUserParkinginfoRepository.GetAll()
                            .Where(e => e.CarId == carId && e.ParkingId == parkingId &&
                                (
                                    //入场时可停车位
                                    (!e.EndTime.HasValue || (e.EndTime.HasValue && e.EndTime.Value >= inTime && (!e.StartTime.HasValue || e.StartTime.Value <= inTime))) ||
                                    //入场至出场期间可停车位
                                    (e.StartTime > inTime && e.StartTime < outTime)
                                )
                            )
                            .Join(_iPlaceRepository.GetAll(), l => l.PlaceId, r => r.Id, (l, r) => new
                            {
                                l.PlaceId,
                                l.StartTime,
                                l.EndTime
                            })
                            .ToListAsync();

                        //有车辆车位信息，否则按临时车处理
                        if (carPlaceList.Count > 0)
                        {
                            //入场时的车位
                            var carPlaceIn = carPlaceList.Where(e => !e.StartTime.HasValue || e.StartTime <= inTime).ToList();
                            //期间新增车位
                            var carPlaceAdd = carPlaceList.Except(carPlaceIn).ToList();

                            //入场时的车位，全部未到期（EndTime为空，或不为空的大于出场时间）
                            if (carPlaceIn.All(e => !e.EndTime.HasValue || e.EndTime.Value >= outTime))
                            {
                                costAndRemark.Remark = "月租车";
                                costAndRemark.Cost = 0;
                                return costAndRemark;
                            }

                            //计算超时的起始时间
                            //车辆出厂时车辆车位相关车辆
                            var carAll = await _iUserParkinginfoRepository.GetAll().Where(e => carPlaceList.Select(s => s.PlaceId).Contains(e.PlaceId) &&
                                    carPlaceList.Any(s => s.PlaceId == e.PlaceId && s.StartTime == e.StartTime && s.EndTime == e.EndTime))
                                .GroupBy(e => e.CarId)
                                .Select(e => e.Key).Join(_iCarInfoRepository.GetAll(), l => l, r => r.Id, (l, r) => r.CarNum)
                                .ToListAsync();

                            //车辆出场时间点在场的月租车辆
                            var carLogList = await _iCarLogHistoryRepository.GetAll()
                                .Where(e => e.ParkingId == parkingId &&
                                    e.CarType != 0 && carAll.Contains(e.CarNum) && e.LogTime < outTime && (e.EndTime >= outTime || !e.EndTime.HasValue))
                                .GroupBy(e => e.CarNum)
                                .Select(e => new { CarNum = e.Key, LogTime = e.Max(s => s.LogTime), Log = e })
                                .ToListAsync();

                            if (carPlaceAdd.Count() > 0)
                            {
                                var minAdd = carPlaceAdd.Min(e => e.StartTime);
                                carLogList = carLogList.Where(e => e.LogTime < minAdd).ToList();
                            }

                            var skipCount = carPlaceIn.Count - carLogList.Count;
                            skipCount = skipCount < 0 ? 0 : skipCount;

                            var minTime = carPlaceIn.Where(e => e.EndTime.HasValue).OrderBy(e => e.EndTime).Skip(skipCount).Take(1).FirstOrDefault();
                            if (minTime != null && minTime.EndTime < outTime)
                            {
                                inTime = minTime.EndTime.Value.AddMinutes(1);
                            }
                            else
                            {
                                return new CostAndRemark { Cost = 0, Remark = "月租车" };
                            }
                        }
                    }

                    //对应车场的计费规则
                    var parkingsBillingScheme = await _repository.FirstOrDefaultAsync(e => e.ParkingId == parkingId && e.Enable == true);
                    if (parkingsBillingScheme == null)
                    {
                        costAndRemark.Remark = "没有计费方案";
                        costAndRemark.Cost = 0;
                        return costAndRemark;
                    }
                    switch (parkingsBillingScheme.ChargeType)
                    {
                        case 0: //按次数收费
                            costAndRemark.Remark = (carType == 0 ? "临时车" : "月租车超期") +
                                "，按次计费。每次" + Math.Round(parkingsBillingScheme.OnePrice, 2).ToString() +
                                "元，免费时长为：" + (parkingsBillingScheme.FreeTime > 0 ? parkingsBillingScheme.FreeTime : 0).ToString() +
                                "，计费期间为" + inTime.ToString("yyyy - MM - dd HH: mm") + " ~" + outTime.ToString("yyyy-MM-dd HH:mm") + "。";

                            costAndRemark.Cost = Math.Round(parkingsBillingScheme.OnePrice, 2);
                            return costAndRemark;
                        case 1: //按时间收费
                                //停留时间
                            var staytime = Math.Truncate((outTime - inTime).TotalMinutes);
                            //计费时间=停留时间-免费时长
                            var billingTime = staytime - (parkingsBillingScheme.FreeTime > 0 ? parkingsBillingScheme.FreeTime : 0);
                            billingTime = billingTime > 0 ? billingTime : 0;
                            //计费周期
                            var intervals = Math.Ceiling(billingTime / parkingsBillingScheme.IntervalTime);
                            //费用
                            costAndRemark.Cost = Convert.ToDecimal(intervals) * parkingsBillingScheme.Univalent;
                            costAndRemark.Cost = costAndRemark.Cost > 0 ? costAndRemark.Cost : 0;
                            //超出封顶按封顶金额，否则实际金额
                            costAndRemark.Cost = parkingsBillingScheme.CappedPrice <= 0 ? costAndRemark.Cost : (costAndRemark.Cost > parkingsBillingScheme.CappedPrice ? parkingsBillingScheme.CappedPrice : costAndRemark.Cost);

                            costAndRemark.Remark = (carType == 0 ? "临时车" : "月租车超期") +
                                "，按时间计费。计费期间：" + inTime.ToString("yyyy-MM-dd HH:mm") + " ~ " + outTime.ToString("yyyy-MM-dd HH:mm") +
                                "，免费时长：" + (parkingsBillingScheme.FreeTime > 0 ? parkingsBillingScheme.FreeTime : 0).ToString() +
                                "分钟，计费标准：" + parkingsBillingScheme.Univalent.ToString("f") + "元/" + parkingsBillingScheme.IntervalTime.ToString() +
                                "分钟，封顶：" + parkingsBillingScheme.CappedPrice.ToString("f") +
                                "元，预交费后" + parkingsBillingScheme.EndFreeTime.ToString() + "分钟内离场不另收费。";

                            return costAndRemark;
                        default: //其他情况
                            costAndRemark.Remark = "没有计费方案";
                            costAndRemark.Cost = 0;
                            return costAndRemark;
                    }
                }
                else
                {
                    //对应车场的计费规则
                    var parkingsBillingScheme = await _repository.FirstOrDefaultAsync(e => e.ParkingId == parkingId && e.Enable == true);
                    if (parkingsBillingScheme == null)
                    {
                        costAndRemark.Remark = "没有计费方案";
                        costAndRemark.Cost = 0;
                        return costAndRemark;
                    }

                    switch (parkingsBillingScheme.ChargeType)
                    {
                        case 0: //按次数收费                        
                            costAndRemark.Cost = 0;
                            costAndRemark.Remark += "，按次收费不另收费";
                            return costAndRemark;
                        case 1: //按时间收费
                            if (Math.Truncate((endTime - startTime).TotalMinutes) <= parkingsBillingScheme.EndFreeTime)
                            {
                                costAndRemark.Cost = 0;
                                costAndRemark.Remark = "未超时";
                                return costAndRemark;
                            }
                            //超期时间
                            var staytime = Math.Truncate((endTime - startTime).TotalMinutes);
                            //计费周期
                            var intervals = Math.Ceiling(staytime / parkingsBillingScheme.IntervalTime);
                            //费用
                            costAndRemark.Cost = Convert.ToDecimal(intervals) * parkingsBillingScheme.Univalent;
                            costAndRemark.Cost = costAndRemark.Cost > 0 ? costAndRemark.Cost : 0;
                            //超出封顶按封顶金额，否则实际金额
                            if (parkingsBillingScheme.CappedPrice > 0 && (costAndRemark.Cost + cost) > parkingsBillingScheme.CappedPrice)
                            {
                                costAndRemark.Cost = (parkingsBillingScheme.CappedPrice - cost);
                            }

                            costAndRemark.Remark +=
                                  "，按时间计费。计费期间：" + startTime.ToString("yyyy-MM-dd HH:mm") + " ~ " + endTime.ToString("yyyy-MM-dd HH:mm") +
                                  "，计费标准：" + parkingsBillingScheme.Univalent.ToString("f") + "元/" + parkingsBillingScheme.IntervalTime.ToString() +
                                  "分钟，封顶：" + parkingsBillingScheme.CappedPrice.ToString("f") +
                                  "元，预交费后" + parkingsBillingScheme.EndFreeTime.ToString() + "分钟内离场不另收费。";

                            return costAndRemark;
                        default: //其他情况
                            costAndRemark.Remark = "没有计费方案";
                            costAndRemark.Cost = 0;
                            return costAndRemark;
                    }
                }
            }
            catch (Exception e)
            {
                costAndRemark.Remark = "系统异常，"+ e.Message;
                costAndRemark.Cost = 0;
                return costAndRemark;
            }
        }
        */
        #endregion

        /// <summary>
        /// 计算收费金额
        /// </summary>
        /// <param name="outTime"></param>
        /// <param name="carLogHistory"></param>
        /// <returns></returns>
        public async Task<CostAndRemark> GetCost(DateTime outTime, CarLogHistory carLogHistory)
        {
            var costAndRemark = new CostAndRemark { Cost = 0, Remark = "" };
            try
            {
                if (!carLogHistory.LogTime.HasValue)
                {
                    costAndRemark.Remark = "没有入场时间";
                    return costAndRemark;
                }
                if (carLogHistory.IsOut) 
                {
                    costAndRemark.Remark = "已出场";
                    return costAndRemark;
                }

                //对应车场的计费规则
                var parkingsBillingScheme = await _repository.FirstOrDefaultAsync(e => e.ParkingId == carLogHistory.ParkingId && e.Enable == true);
                if (parkingsBillingScheme == null)
                {
                    costAndRemark.Remark = "没有计费方案";
                    costAndRemark.Cost = 0;
                    return costAndRemark;
                }

                outTime = Convert.ToDateTime(outTime.ToString("yyyy-MM-dd HH:mm"));  
                var inTime = Convert.ToDateTime(carLogHistory.LogTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                if (outTime < inTime)
                {
                    costAndRemark.Remark = "出场时间小于等于入场时间，入场时间：" + inTime.ToString("yyyy-MM-dd HH:mm:ss") + ",出场时间：" + outTime.ToString("yyyy-MM-dd HH:mm:ss");
                    return costAndRemark;
                }

                var paymethod = await _paymethodRepository.GetAll()
                    .GroupBy(e => e.CarLogHistoryId)
                    .Where(e => e.Key == carLogHistory.Id)
                    .Select(e => new { LastCreationTime = e.Max(m => m.CreationTime), SumCost = e.Sum(s => s.Cost) })
                    .FirstOrDefaultAsync();
                var hasPaymethod = (paymethod == null || paymethod.SumCost == 0) ? false : true;

                //没有交费信息
                if (!hasPaymethod)
                {
                    //计费原则：
                    //1. 进场为临时车时按临时车计费，
                    //2. 进场为月租车时，如果未超期费用为0，如果超期按临时车收费，收费开始时间为车位到期时间加一分钟。            
                    if (carLogHistory.CarType != 0)
                    {
                        //车辆入场或出场时的有效月租车车位信息
                        var carPlaceList = await _iUserParkinginfoRepository.GetAll()
                            .Where(e => e.CarId == carLogHistory.CarId && e.ParkingId == carLogHistory.ParkingId &&
                                (
                                    //入场时可停车位
                                    (!e.EndTime.HasValue || (e.EndTime.HasValue && e.EndTime.Value >= inTime && (!e.StartTime.HasValue || e.StartTime.Value <= inTime))) ||
                                    //入场至出场期间可停车位
                                    (e.StartTime > inTime && e.StartTime < outTime)
                                )
                            )
                            .Join(_iPlaceRepository.GetAll(), l => l.PlaceId, r => r.Id, (l, r) => new
                            {
                                l.PlaceId,
                                l.StartTime,
                                l.EndTime
                            })
                            .ToListAsync();

                        //有车辆车位信息，否则按临时车处理
                        if (carPlaceList.Count > 0)
                        {
                            //入场时的车位
                            var carPlaceIn = carPlaceList.Where(e => !e.StartTime.HasValue || e.StartTime <= inTime).ToList();
                            //期间新增车位
                            var carPlaceAdd = carPlaceList.Except(carPlaceIn).ToList();

                            //入场时的车位，全部未到期（EndTime为空，或不为空的大于出场时间）
                            if (carPlaceIn.All(e => !e.EndTime.HasValue || e.EndTime.Value >= outTime))
                            {
                                costAndRemark.Remark = "月租车";
                                costAndRemark.Cost = 0;
                                return costAndRemark;
                            }

                            //计算超时的起始时间
                            //车辆出厂时车辆车位相关车辆
                            var carAll = await _iUserParkinginfoRepository.GetAll().Where(e => carPlaceList.Select(s => s.PlaceId).Contains(e.PlaceId) &&
                                    carPlaceList.Any(s => s.PlaceId == e.PlaceId && s.StartTime == e.StartTime && s.EndTime == e.EndTime))
                                .Select(e => e.CarId)
                                .Distinct()
                                .ToListAsync();

                            //车辆出场时间点在场的月租车辆
                            var carLogList = await _iCarLogHistoryRepository.GetAll()
                                .Where(e => e.ParkingId == carLogHistory.ParkingId &&
                                    e.CarType != 0 && carAll.Contains(e.CarId) && e.LogTime < outTime && (e.EndTime >= outTime || !e.EndTime.HasValue))
                                .GroupBy(e => e.CarId)
                                .Select(e => new { CarId = e.Key, LogTime = e.Max(s => s.LogTime), Log = e })
                                .ToListAsync();

                            if (carPlaceAdd.Count() > 0)
                            {
                                var minAdd = carPlaceAdd.Min(e => e.StartTime);
                                carLogList = carLogList.Where(e => e.LogTime < minAdd).ToList();
                            }

                            var skipCount = carPlaceIn.Count - carLogList.Count;
                            skipCount = skipCount < 0 ? 0 : skipCount;

                            var minTime = carPlaceIn.Where(e => e.EndTime.HasValue).OrderBy(e => e.EndTime).Skip(skipCount).Take(1).FirstOrDefault();
                            if (minTime != null && minTime.EndTime < outTime)
                            {
                                inTime = minTime.EndTime.Value.AddMinutes(1);
                            }
                            else
                            {
                                return new CostAndRemark { Cost = 0, Remark = "月租车" };
                            }
                        }
                    }
                }

                switch (parkingsBillingScheme.ChargeType)
                {
                    case 0: //按次数收费
                        if (hasPaymethod)
                        {
                            costAndRemark.Cost = 0;
                            costAndRemark.Remark = "预交费超期，按次收费不另收费";
                            return costAndRemark;
                        }
                        else
                        {
                            costAndRemark.Remark = (carLogHistory.CarType == 0 ? "临时车" : "月租车超期") +
                                "，按次计费。每次" + Math.Round(parkingsBillingScheme.OnePrice, 2).ToString() +
                                "元，免费时长为：" + (parkingsBillingScheme.FreeTime > 0 ? parkingsBillingScheme.FreeTime : 0).ToString() +
                                "，计费期间为" + inTime.ToString("yyyy - MM - dd HH: mm") + " ~" + outTime.ToString("yyyy-MM-dd HH:mm") + "。";

                            costAndRemark.Cost = Math.Round(parkingsBillingScheme.OnePrice, 2);
                            return costAndRemark;
                        }
                    case 1: //按时间收费
                        if (hasPaymethod)
                        {
                            if (Math.Truncate((outTime - paymethod.LastCreationTime.AddMinutes(1)).TotalMinutes) <= parkingsBillingScheme.EndFreeTime)
                            {
                                costAndRemark.Cost = 0;
                                costAndRemark.Remark = "未超时";
                                return costAndRemark;
                            }
                            //超期时间
                            var staytime = Math.Truncate((outTime - paymethod.LastCreationTime.AddMinutes(1)).TotalMinutes);
                            //计费周期
                            var intervals = Math.Ceiling(staytime / parkingsBillingScheme.IntervalTime);
                            //费用
                            costAndRemark.Cost = Convert.ToDecimal(intervals) * parkingsBillingScheme.Univalent;
                            costAndRemark.Cost = costAndRemark.Cost > 0 ? costAndRemark.Cost : 0;
                            //超出封顶按封顶金额，否则实际金额
                            if (parkingsBillingScheme.CappedPrice > 0 && (costAndRemark.Cost + paymethod.SumCost) > parkingsBillingScheme.CappedPrice)
                            {
                                costAndRemark.Cost = (parkingsBillingScheme.CappedPrice - paymethod.SumCost);
                            }

                            costAndRemark.Remark +=
                                  "，按时间计费。计费期间：" + paymethod.LastCreationTime.AddMinutes(1).ToString("yyyy-MM-dd HH:mm") + " ~ " + outTime.ToString("yyyy-MM-dd HH:mm") +
                                  "，计费标准：" + parkingsBillingScheme.Univalent.ToString("f") + "元/" + parkingsBillingScheme.IntervalTime.ToString() +
                                  "分钟，封顶：" + parkingsBillingScheme.CappedPrice.ToString("f") +
                                  "元，预交费后" + parkingsBillingScheme.EndFreeTime.ToString() + "分钟内离场不另收费。";

                            return costAndRemark;
                        }
                        else
                        {
                            //停留时间
                            var staytime = Math.Truncate((outTime - inTime).TotalMinutes);
                            //计费时间=停留时间-免费时长
                            var billingTime = staytime - (parkingsBillingScheme.FreeTime > 0 ? parkingsBillingScheme.FreeTime : 0);
                            billingTime = billingTime > 0 ? billingTime : 0;
                            //计费周期
                            var intervals = Math.Ceiling(billingTime / parkingsBillingScheme.IntervalTime);
                            //费用
                            costAndRemark.Cost = Convert.ToDecimal(intervals) * parkingsBillingScheme.Univalent;
                            costAndRemark.Cost = costAndRemark.Cost > 0 ? costAndRemark.Cost : 0;
                            //超出封顶按封顶金额，否则实际金额
                            costAndRemark.Cost = parkingsBillingScheme.CappedPrice <= 0 ? costAndRemark.Cost : (costAndRemark.Cost > parkingsBillingScheme.CappedPrice ? parkingsBillingScheme.CappedPrice : costAndRemark.Cost);

                            costAndRemark.Remark = (carLogHistory.CarType == 0 ? "临时车" : "月租车超期") +
                                "，按时间计费。计费期间：" + inTime.ToString("yyyy-MM-dd HH:mm") + " ~ " + outTime.ToString("yyyy-MM-dd HH:mm") +
                                "，免费时长：" + (parkingsBillingScheme.FreeTime > 0 ? parkingsBillingScheme.FreeTime : 0).ToString() +
                                "分钟，计费标准：" + parkingsBillingScheme.Univalent.ToString("f") + "元/" + parkingsBillingScheme.IntervalTime.ToString() +
                                "分钟，封顶：" + parkingsBillingScheme.CappedPrice.ToString("f") +
                                "元，预交费后" + parkingsBillingScheme.EndFreeTime.ToString() + "分钟内离场不另收费。";

                            return costAndRemark;
                        }
                    default: //其他情况
                        costAndRemark.Remark = "没有计费方案";
                        costAndRemark.Cost = 0;
                        return costAndRemark;
                }
            }
            catch (Exception e)
            {
                costAndRemark.Remark = "系统异常，" + e.Message;
                costAndRemark.Cost = 0;
                return costAndRemark;
            }
        }

        /// <summary>
        /// 启用计费方案,同时关闭本停车场其他计费方案
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> UpdateEnable(long id) 
        {
            var parkingsBillingScheme = await _repository.FirstOrDefaultAsync(id);
            if (parkingsBillingScheme == null)
            {
                return "启用失败，未找到方案信息，请刷新页面再操作";
            }

            var parkingBillingSchemeList = await _repository.GetAll().Where(e => e.ParkingId == parkingsBillingScheme.ParkingId).ToListAsync();
            foreach (var item in parkingBillingSchemeList)
            {
                item.Enable = false;
                await _repository.UpdateAsync(item);
            } 

            parkingsBillingScheme.Enable = true;
            await _repository.UpdateAsync(parkingsBillingScheme);

            return "";
        }

    }

    public class CostAndRemark
    { 
        public decimal Cost { get; set; }
        public string Remark { get; set; }
    }
}
